package org.brownflat.watergate.domain;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>&copy Rupert Jones 2012</p>
 *
 * @author rup
 */
@Entity
@Table(name = "wgate_commit")
public class Commit extends BaseEntity {

    @LazyCollection(LazyCollectionOption.FALSE)
    @OneToMany(mappedBy="commit", orphanRemoval = true)
    @Access(value= AccessType.PROPERTY)
    @Cascade( { org.hibernate.annotations.CascadeType.ALL } )
    private List<CommitElement> commitElements = new ArrayList<CommitElement>();

    @ManyToOne
    @JoinColumn(name="branch")
    private Branch branch;

    @Column(name="message")
    private String message;

    @Column
    private long revision;

    @ManyToOne
    @JoinColumn(name="user_id")
    private User user;

    @Column(name="review_trig_scanned")
    private boolean reviewTriggerScanned;

    public Branch getBranch() {
        return branch;
    }

    public void setBranch(Branch branch) {
        this.branch = branch;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public long getRevision() {
        return revision;
    }

    public void setRevision(long revision) {
        this.revision = revision;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public List<CommitElement> getCommitElements() {
        return commitElements;
    }

    public void setCommitElements(List<CommitElement> commitElements) {
        this.commitElements = commitElements;
    }

    public void addCommitElement(CommitElement commitElement) {
        this.commitElements.add(commitElement);
    }

    public int countCommitElements() {
        return commitElements.size();
    }

    public boolean isReviewTriggerScanned() {
        return reviewTriggerScanned;
    }

    public void setReviewTriggerScanned(boolean reviewTriggerScanned) {
        this.reviewTriggerScanned = reviewTriggerScanned;
    }

    public int countCommitElements(CommitElementType type) {
        int count = 0;

        for(CommitElement commitElement : commitElements) {
            if(commitElement.is(type))  {
                count++;
            }
        }
        return count;
    }
}
